"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop -n nodeid故障测试-$GAUSSHOME/bin权限
Create At   : 2025-03-1
Owner       : sungang14
Description :
    1: 执行cm_ctl stop -n nodeid命令,过程中修改$gausshome/bin权限为000;
    cm_ctl stop -n nodeid
    chmod 000 $gausshome/bin
    2: 恢复$gausshome/bin权限;
    chmod 700 $gausshome/bin
    3: 再次执行cm_ctl stop -n nodeid命令;
    cm_ctl stop -n nodeid
    4: 启动节点;
    cm_ctl start -n nodeid
Expect      :
    1: 修改成功,stop失败
    2: 成功
    3: 成功
    4: 成功
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.sta1_root = Node('Standby1Root')
        self.constant = Constant()
        self.common = Common()

    def test_cm(self):
        text = '----step1:执行cm_ctl stop -n nodeid命令,过程中修改$gausshome/bin权限为000;; expect:修改成功,stop失败----'
        self.log.info(text)
        stop_cmd = f'source {macro.DB_ENV_PATH};' \
                   f'cm_ctl stop -n 2 ;'
        self.log.info(stop_cmd)


        chown_cmd = f"chmod 000 {macro.GAUSSHOME_BIN_PATH}"
        chown_thread = ComThread(self.sta1_root.sh, args=(chown_cmd,))
        chown_thread.setDaemon(True)
        chown_thread.start()
        chown_thread.join(10)
        chown_res = chown_thread.get_result().result()
        self.log.info(chown_res)
        self.assertEquals("", chown_res, "执行失败:" + text)
        self.log.info('----获取stop结果----')
        stop_thread = ComThread(self.pri_user.sh, args=(stop_cmd,))
        stop_thread.setDaemon(True)
        stop_thread.start()
        stop_thread.join(40)
        result = stop_thread.get_result().result()
        self.log.info(result)
        self.assertIn("cm_ctl: the cluster static config file does not exist on the node: 2.", result,
                       "执行失败:" + text)
        text = '执行cm_ctl query -Civ查询node状态;expect:状态为Down    Unknown,未成功stop----'
        self.log.info(text)
        time.sleep(10)
        query_cmd = f"source {macro.DB_ENV_PATH};cm_ctl query -Civ"
        kill_res = self.common.get_sh_result(self.pri_user, query_cmd)
        self.assertIn(f"6002     S Down    Unknown", kill_res, "执行失败:" + text)

        text = '----step2:恢复$gausshome/bin权限 expect:成功----'
        self.log.info(text)
        self.chown_cmd = f"chmod 700 {macro.GAUSSHOME_BIN_PATH}"
        self.log.info(self.chown_cmd)
        chown_res = self.common.get_sh_result(self.sta1_root, self.chown_cmd)
        self.assertIn("", chown_res, "执行失败:" + text)

        text = '----step3:再次执行cm_ctl stop -n nodeid命令; expect:执行停止----'
        self.log.info(text)
        self.log.info(stop_cmd)
        stop_msg = self.common.get_sh_result(self.pri_user, stop_cmd)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_msg,
                      "执行失败:" + text)

        text = '----step4:查看集群状态; expect:集群状态异常，备节点停止----'
        self.log.info(text)
        time.sleep(5)
        show_status = f'source {macro.DB_ENV_PATH};cm_ctl query -Civ'
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                      "执行失败:" + text)
        self.assertIn(f'6002     S Down    Unknown', status_msg,
                      "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '-----step5:启动节点; expect:成功-----'
        self.log.info(text)
        self.common.get_sh_result(self.sta1_root, self.chown_cmd)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'cm_ctl start -n 2 ;'
        self.log.info(start_cmd)
        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)
        self.assertIn(self.constant.cm_start_node_success_msg, start_msg,
                      "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
